package com.example.shaoying.yuejian.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;

import com.example.shaoying.yuejian.R;
import com.example.shaoying.yuejian.fragment.clubeFragment.Clube01Fragment;
import com.example.shaoying.yuejian.fragment.clubeFragment.Clube02Fragment;
import com.example.shaoying.yuejian.fragment.mainFragment.ActivityFragment;
import com.example.shaoying.yuejian.fragment.mainFragment.GameFragment;
import com.example.shaoying.yuejian.fragment.mainFragment.TeacherFragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;


public class MainFragment extends Fragment implements View.OnClickListener {

    private List<ImageView> images;
    private int[] imgResource={R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d};
    private ViewPager train_vp;
    private View view;

/*    private LinearLayout Lteacher;
    private LinearLayout Lgame;
    private LinearLayout Lhuodong;*/

    private Fragment Fteacher;
    private Fragment Fhuodong;
    private Fragment Fgame;

    private RadioButton Bhuodong;
    private RadioButton Bteacher;
    private RadioButton Bgame;

    //当前图片的位置
    private int current;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       view=inflater.inflate(R.layout.tab03, container, false);
        init();
        event();
        lunbo();
        setSelect(0);
        return view ;
    }
    private void event() {
        Bhuodong.setOnClickListener( this);
        Bteacher.setOnClickListener(this);
        Bgame.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.huodong:
                setSelect(0);
                break;
            case R.id.teacher:
                setSelect(1);
                break;
            case R.id.game:
                setSelect(2);
                break;
            default:
                break;
        }
    }
    private void setSelect(int i)
    {
        FragmentManager fm =getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i)
        {
            case 0:
                if (Fhuodong == null)
                {
                    Fhuodong = new ActivityFragment();
                    transaction.add(R.id.id_tab03, Fhuodong);

                } else
                {
                    transaction.show(Fhuodong);
                }

                break;
            case 1:
                if (Fteacher == null)
                {
                    Fteacher = new TeacherFragment();
                    transaction.add(R.id.id_tab03, Fteacher);
                } else
                {
                    transaction.show(Fteacher);

                }

                break;
            case 2:
                if (Fgame == null)
                {
                    Fgame = new GameFragment();
                    transaction.add(R.id.id_tab03, Fgame);
                } else
                {
                    transaction.show(Fgame);

                }

                break;

            default:
                break;
        }
        transaction.commit();
    }
    private void lunbo() {
        train_vp.setAdapter(pageadapter);
    }

    public void  init(){

        Bhuodong= (RadioButton) view.findViewById(R.id.huodong);
        Bteacher= (RadioButton) view.findViewById(R.id.teacher);
        Bgame= (RadioButton) view.findViewById(R.id.game);

        train_vp=(ViewPager)view.findViewById(R.id.train_vp);

        //创建List对象，用于装载ImageView对象
        images=new ArrayList<ImageView>();
        for (int i=0;i<imgResource.length;i++)
        {
            ImageView imageView=new ImageView(getActivity());
            imageView.setBackgroundResource(imgResource[i]);
            images.add(imageView);
        }
    }

    private void hideFragment(FragmentTransaction transaction)
    {
        if (Fhuodong != null)
        {
            transaction.hide(Fhuodong);
        }
        if (Fteacher != null)
        {
            transaction.hide(Fteacher);
        }
        if (Fgame != null)
        {
            transaction.hide(Fgame);
        }
    }









    PagerAdapter pageadapter=new PagerAdapter() {
        @Override
        public int getCount() {
            return images.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(images.get(position));
            return images.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(images.get(position));
        }
    };



    class  PageTask implements Runnable{
        @Override
        public void run() {
            current=(current+1)%imgResource.length;
            handler.sendEmptyMessage(current);
        }
    }
    private Handler handler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            train_vp.setCurrentItem(current);
        };
    };

    @Override
    public void onStart() {
        super.onStart();
        Timer timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                current=(current+1)%imgResource.length;
                handler.sendEmptyMessage(current);
            }
        },2000,2000);
    }


}
